#include <QDebug>
#include "xmlhandler.h"

xmlHandler::xmlHandler(QMap<QString, QString>& _contacts) : entry(false), title(false), contacts(_contacts)
{
}

bool xmlHandler::startElement(const QString & namespaceURI,
                       const QString & localName,
                       const QString & qName,
                       const QXmlAttributes & atts ) {
    Q_UNUSED(namespaceURI)
    Q_UNUSED(qName)
    if(localName == entry_tag) entry = true;

    if(entry) {
        if(localName == user_name_tag) title = true;
        else if(localName == user_email_tag) {
            if(username.isEmpty()) username = atts.value(user_email_attr);
            contacts[username] = atts.value(user_email_attr);
            username.clear();
        }
    }
    return true;
}

bool xmlHandler::endElement(const QString & namespaceURI,
                     const QString & localName,
                     const QString & qName) {
    Q_UNUSED(namespaceURI)
    Q_UNUSED(qName)
    if(localName == entry_tag) entry = false;
    if(entry) {
        if(localName == user_name_tag) title = false;
    }
    return true;
}

bool xmlHandler::characters(const QString & ch) {
    if(entry) {
        if(title) { username = ch; }
    }
    return true;
}
